package com.yisu.expressway.scancode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.BarcodeFormat;
import com.yisu.expressway.R;
import com.yisu.expressway.main_page.model.HomeBannerObj;
import com.yisu.expressway.model.ServiceDistrictObj;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.activity.GoodsPurchaseActivity;
import com.yisu.expressway.scancode.model.Code2ProductObj;
import com.yisu.expressway.shopping_mall.YouZanWebActivity;
import com.yisu.expressway.ui.loading.CustomProgressDialog;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.github.xudaojie.qrcodelib.CaptureActivity;


public class ScanCodeActivity extends CaptureActivity {

    protected Activity mActivity = this;
    private final static String RESULT_HEAD = "http";
    CustomProgressDialog mLoadingDialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        mActivity = this;
        super.onCreate(savedInstanceState);
        mLoadingDialog = new CustomProgressDialog(this);
    }

    @Override
    protected void handleResult(String resultString, BarcodeFormat barcodeFormat) {
        if (StringUtil.isNullOrEmpty(resultString)) {
            Toast.makeText(mActivity, io.github.xudaojie.qrcodelib.R.string.scan_failed, Toast.LENGTH_SHORT).show();
            restartPreview();
        } else {
            // TODO: 16/9/17 ...
            if(barcodeFormat == BarcodeFormat.QR_CODE) {
                if (resultString.startsWith(RESULT_HEAD)) {
                    YouZanWebActivity.launch(mActivity, resultString, "");
                } else {
                    ToastUtil.displayShortToastMsg(mActivity, "不是路伯伯家的商品");
                }
                finish();
            }else {
                getCodeUrl(resultString);
            }
        }
    }

//    @Override
//    protected void handleResult(String resultString) {
//        if (StringUtil.isNullOrEmpty(resultString)) {
//            Toast.makeText(mActivity, R.string.scan_failed, Toast.LENGTH_SHORT).show();
//            restartPreview();
//        } else {
//            // TODO: 16/9/17 ...
//            if (resultString.startsWith(RESULT_HEAD)) {
//                YouZanWebActivity.launch(mActivity, resultString, "");
//                finish();
//            } else {
//                ToastUtil.displayShortToastMsg(mActivity, "非路伯伯家商品");
//                restartPreview();
//            }
//        }
//    }

    private void getCodeUrl(String code){
        showLoading();

        Map<String, Object> params = new HashMap();
        params.put("productBarcode", code);

        ApiClientHelper.postApi(ApiSetting.getCodeUrl(), new TypeToken<Code2ProductObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<Code2ProductObj>>() {
            @Override
            public void onResponse(ApiResult<Code2ProductObj> result) {

                dismissLoading();
                if (!result.success) {
                    ToastUtil.displayShortToastMsg(mActivity,"非路伯伯家商品");
                    finish();
                    return;
                }

                Code2ProductObj item = result.getResult();
                if(item != null) {
                    jump2NoticeLinkPage(item);
                    finish();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                finish();
                ToastUtil.displayShortToastMsg(ScanCodeActivity.this, volleyError.getMessage());
            }
        }, this);
    }

    private void jump2NoticeLinkPage(Code2ProductObj item){
        switch (item.productType) {
            case Code2ProductObj.PRODUCT_TYPE_ONEDOLLER:
                GoodsPurchaseActivity.launch(this, Long.valueOf(item.productLink));
                break;
            case Code2ProductObj.PRODUCT_TYPE_YOUZAN:
                YouZanWebActivity.launch(this, item.productLink, "路伯伯商城");
                break;
            default:
                break;
        }
    }

    /**
     * 显示默认loading
     */
    public void showLoading() {
        if(mLoadingDialog != null){
            mLoadingDialog.show();
            mLoadingDialog.showLoading();
        }
    }

    /**
     * 关闭loading
     */
    public void dismissLoading() {
        if(mLoadingDialog != null){
            mLoadingDialog.dismissLoading();
            mLoadingDialog.dismiss();
        }
    }

}
